<template>
  <div>
    <div v-for="(item, index) in treeData" :key="index" class="mg_t10">
      <div class="tree-item" @click="item.isDept ? expand(item) : chose(item, treeData)">
        <span>
          <img
            v-if="item.isDept"
            src="@/assets/icons/icon2.png"
            style="width:18px;height:18px;margin-right:5px;margin-top:2px;margin-bottom:8px"
          />
          <img v-else src="@/assets/icons/person1.png" style="width:30px;height:30px;margin-right:10px" />
          {{ item.name }}
          {{ item.phone ? "(" + item.phone + ")" : "" }}
        </span>

        <i
          v-if="item.isSelected"
          v-on:click.stop="chose(item, treeData)"
          style="font-size: 19px;"
          class="iconfont icon-check-circle-fill primary_color"
        >
        </i>
        <img v-else v-on:click.stop="chose(item, treeData)" src="@/assets/icons/未选择.png" style="width:18px;height:18px;" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "TreeNode",
  props: {
    treeData: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    chose(item, data) {
      item.isSelected = !item.isSelected;
      this.$emit("change", item);
    },
    expand(item) {
      if (!item.isExpand) {
        this.$emit("expand", item);
      }

      var that = this;
      setTimeout(function() {
        item.isExpand = !item.isExpand;
        if (item.isExpand) {
          that.$emit("expand", item);
        }
      }, "10");
    }
  }
};
</script>
<style lang="scss" scoped>
.tree-item {
  display: flex;
  justify-content: space-between;
  flex-direction: row;
  align-items: center;
  cursor: pointer;
}
</style>
